package com.bestcem.xm.user.grpc.client.converter;

import com.bestcem.xm.qdes.grpc.v1.services.Project;
import com.bestcem.xm.qdes.grpc.v1.services.ProjectEntry;
import com.bestcem.xm.user.grpc.client.dto.ProjectDTO;
import com.bestcem.xm.user.grpc.client.dto.ProjectEntryDTO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * qdes相关类转换
 *
 * @author qiongyu.cui <qiongyu.cui@idiaoyan.com>
 * @version v1.0
 * @date 2022/7/12 17:18
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class UserBaseQdesGrpcConverter {

    public static ProjectDTO responseToProjectDTO(Project project) {
        if (Objects.isNull(project)) {
            return null;
        }
        ProjectDTO projectDTO = new ProjectDTO();
        projectDTO.setReleaseVersion(project.getReleaseVer());
        projectDTO.setStatus(project.getStatusValue());
        projectDTO.setPtype(project.getPtypeValue());
        projectDTO.setCopyDemo(project.getIsCopyDemo());
        if (CollectionUtils.isNotEmpty(project.getGroupListList())) {
           projectDTO.setGroupList(project.getGroupListList());
        }
        if (project.getId() != null) {
            projectDTO.setId(project.getId());
        }
        if (project.getCode() != null) {
            projectDTO.setCode(project.getCode());
        }
        if (project.getTitle() != null) {
            projectDTO.setTitle(project.getTitle());
        }
        if (project.getCreator() != null) {
            projectDTO.setCreator(project.getCreator());
        }
        if (project.getCompanyId() != null) {
            projectDTO.setCompanyId(project.getCompanyId());
        }
        projectDTO.setVersion(project.getVersion());
        if (project.getStructId() != null) {
            projectDTO.setStructId(project.getStructId());
        }
        if (project.getLogicId() != null) {
            projectDTO.setLogicId(project.getLogicId());
        }
        if (project.getCustomAttr() != null) {
            projectDTO.setCustomAttr(project.getCustomAttr());
        }
        if (project.getSource() != null) {
            projectDTO.setSource(project.getSource());
        }
        if (project.getExtId() != null) {
            projectDTO.setExtId(project.getExtId());
        }
        projectDTO.setTimerExpire(project.getTimerExpire());
        projectDTO.setWorktableInfo(project.getWorktableInfo());
        projectDTO.setAnswerLanguage(project.getAnswerLanguage());
        return projectDTO;
    }

    public static ProjectEntryDTO projectEntryGrpc2Dto(ProjectEntry projectEntry) {
        if (projectEntry == null) {
            return null;
        }
        ProjectEntryDTO projectEntryDTO = new ProjectEntryDTO();
        projectEntryDTO.setCategory(projectEntry.getCategoryValue());
        List<Integer> list = projectEntry.getPermissionsList();
        if (CollectionUtils.isNotEmpty(list)) {
            projectEntryDTO.setPermissions(list);
        }
        projectEntryDTO.setPermType(projectEntry.getPermTypeValue());
        projectEntryDTO.setStatus(projectEntry.getStatusValue());
        projectEntryDTO.setBadgeStatus(projectEntry.getBadgeStatusValue());
        if (projectEntry.getId() != null) {
            projectEntryDTO.setId(projectEntry.getId());
        }
        if (projectEntry.getOrgId() != null) {
            projectEntryDTO.setOrgId(projectEntry.getOrgId());
        }
        if (projectEntry.getProjectId() != null) {
            projectEntryDTO.setProjectId(projectEntry.getProjectId());
        }
        if (projectEntry.getUserId() != null) {
            projectEntryDTO.setUserId(projectEntry.getUserId());
        }
        if (projectEntry.getTitle() != null) {
            projectEntryDTO.setTitle(projectEntry.getTitle());
        }
        if (projectEntry.getFolderId() != null) {
            projectEntryDTO.setFolderId(projectEntry.getFolderId());
        }
        if (projectEntry.getCreatorId() != null) {
            projectEntryDTO.setCreatorId(projectEntry.getCreatorId());
        }
        projectEntryDTO.setTopOrder(projectEntry.getTopOrder());
        return projectEntryDTO;
    }

    public static List<ProjectEntryDTO> responseToProjectEntryDTOs(List<ProjectEntry> projectEntryList) {
        if (projectEntryList == null) {
            return Collections.emptyList();
        }
        List<ProjectEntryDTO> list = new ArrayList<>(projectEntryList.size());
        for (ProjectEntry projectEntry : projectEntryList) {
            list.add(projectEntryGrpc2Dto(projectEntry));
        }
        return list;
    }

    public static ProjectEntry projectEntryDto2Grpc(ProjectEntryDTO projectEntryDTO) {
        if (projectEntryDTO == null) {
            return null;
        }
        ProjectEntry.Builder projectEntry = ProjectEntry.newBuilder();
        if (projectEntry.getPermissionsList() != null) {
            List<Integer> list = projectEntryDTO.getPermissions();
            if (list != null) {
                projectEntry.addAllPermissions(list);
            }
        }
        if (projectEntryDTO.getId() != null) {
            projectEntry.setId(projectEntryDTO.getId());
        }
        if (projectEntryDTO.getOrgId() != null) {
            projectEntry.setOrgId(projectEntryDTO.getOrgId());
        }
        if (projectEntryDTO.getProjectId() != null) {
            projectEntry.setProjectId(projectEntryDTO.getProjectId());
        }
        if (projectEntryDTO.getUserId() != null) {
            projectEntry.setUserId(projectEntryDTO.getUserId());
        }
        if (projectEntryDTO.getTitle() != null) {
            projectEntry.setTitle(projectEntryDTO.getTitle());
        }
        if (projectEntryDTO.getCategory() != null) {
            projectEntry.setCategoryValue(projectEntryDTO.getCategory());
        }
        if (projectEntryDTO.getPermType() != null) {
            projectEntry.setPermTypeValue(projectEntryDTO.getPermType());
        }
        if (projectEntryDTO.getFolderId() != null) {
            projectEntry.setFolderId(projectEntryDTO.getFolderId());
        }
        if (projectEntryDTO.getStatus() != null) {
            projectEntry.setStatusValue(projectEntryDTO.getStatus());
        }
        if (projectEntryDTO.getBadgeStatus() != null) {
            projectEntry.setBadgeStatusValue(projectEntryDTO.getBadgeStatus());
        }
        if (projectEntryDTO.getCreatorId() != null) {
            projectEntry.setCreatorId(projectEntryDTO.getCreatorId());
        }
        if (projectEntryDTO.getTopOrder() != null) {
            projectEntry.setTopOrder(projectEntryDTO.getTopOrder());
        }
        return projectEntry.build();
    }


}
